﻿(function (ekb, $, undefined) {
    ekb.disable = function (selector) {
        if ($(selector) !== undefined) {
            $(selector).addClass('disabled');
        }
    };
    ekb.enable = function (selector) {
        if ($(selector) !== undefined) {
            $(selector).removeClass('disabled');
        }
    };
    ekb.post = function (action, data, before, complete, success, error) {
        $.ajax({
            url: action,
            type: 'POST',
            dataType: 'json',
            data: data,
            contentType: 'application/json',
            beforeSend: before
        }).done(success).fail(error).always(complete);
    };
    ekb.showImage = function (id) {
        if ($('img#' + id) !== undefined) { $('img#' + id).show(100); }
    };
    ekb.showOverlay = function (id) {
        if ($('div#' + id + 'Overlay') !== undefined) {
            $('div#' + id + 'Overlay').removeClass('hidden');
        }
    };
    ekb.hideOverlay = function (id) {
        if ($('div#' + id + 'Overlay') !== undefined) {
            $('div#' + id + 'Overlay').addClass('hidden');
        }
    };
    ekb.appendText = function (selector, text, slide) {
        if (slide === true) { $(selector).hide(); }
        $(selector).empty();
        $(selector).append(text);
        if (slide === true) { $(selector).slideDown(500); }
    };
    ekb.showcontent = function (selector, effect, object, duration) { $(selector).show(effect, object, duration); };
    ekb.appendResult = function (url, selector, args, overlay) {
        if(overlay === true) { this.showOverlay(selector); }
        this.post(
            url,
            JSON.stringify(args),
            null, /* Before */
            function (response) {
                ekb.appendText('div#' + selector, response.responseText, false);
                if (overlay === true) { ekb.hideOverlay(selector); }
            },
            null /* Sucess*/,
            null /* Error */);
        return this;
    };
    ekb.loadKills = function (url, index) {
        if ($('a#next').hasClass('disabled') || $('a#previous').hasClass('disabled')) return this;
        this.disable('a#next');
        this.disable('a#previous');
        this.disable('a#listNavLink');
        this.post(url,
            JSON.stringify({ index: index }),
            this.showImage('listLoading'),
            function (result) {
                ekb.appendText('div#killmails', result.responseText, false);
                ekb.enable('a#next');
                ekb.enable('a#previous');
                ekb.enable('a#listNavLink');
            },
            null);
        return this;
    };
    ekb.loadList = function (url, id, index) {
        if ($('a#next').hasClass('disabled') || $('a#previous').hasClass('disabled')) return;
        this.disable('a#next');
        this.disable('a#previous');

        this.post(url,
            JSON.stringify({ id: id, index: index }),
            this.showImage('listLoading'),
            function (result) {
                ekb.appendText('div#listContent', result.responseText, false);
                ekb.enable('a#next');
                ekb.enable('a#previous');
                ekb.showImage('loading');
//                var currentindex = parseInt(document.getElementById('index').value);
//                if (currentindex === -1) {
//                    $('a#next').addClass('hidden');
//                    $('img#imgNext').addClass('hidden');
//                    $('a#previous').removeClass('hidden');
//                    $('img#imgPrevious').removeClass('hidden');
//                } else if (currentindex === 0) {
//                    $('a#previous').addClass('hidden');
//                    $('img#imgPrevious').addClass('hidden');
//                } else {
//                    $('a#previous').removeClass('hidden');
//                    $('img#imgPrevious').removeClass('hidden');
//                }
            });
    };
} (window.evekillboard = window.evekillboard || {}, jQuery));